package com.lagou.task01;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class StudentEditor {

    // 学生类编辑器，实现增删改查

    // 数据源
    private List<Student> studentList = new LinkedList<>();
    // 输入
    public Scanner sc = new Scanner(System.in);

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    // 增加
    public void add(){


        Student s = new Student();

        System.out.println("请输入姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入年龄");
        int age = sc.nextInt();
//        while (age < 1){
//            System.out.println("年龄不合理，请重新输入:");
//            age = sc.nextInt();
//        }

        while (age < 1) {
            try {
                s.setAge(age);
            } catch (StudentAgeException e) {
                e.printStackTrace();
                System.out.println("请重新输入年龄:");
                age = sc.nextInt();
            }
        }

        boolean vaildStuId = false;
        int sId = 0;

        while (!vaildStuId){
            System.out.println("请输入学号");
            sId = sc.nextInt();

            try {
                s.setsId(sId);
                vaildStuId = true;
                for (Student st : studentList) {
                    if (st.getsId() == sId){
                        vaildStuId = false;
                        System.out.println("该学号已被占用");
                        break;
                    }
                }
            }catch (StudentIdException e){
                vaildStuId = false;
                e.printStackTrace();
            }
        }

        try {
            s = new Student(name,age,sId);
            boolean ret = studentList.add(s);
            System.out.println(ret ? "添加成功" : "添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("添加失败");
        }
    }



    // 删除
    public void delete(){

        if (noStudent())  return;

        System.out.println("请输入需要删除学生的学号");
        traverse();
        int sId = sc.nextInt();

        Student tmpS = null;
        for (Student s : studentList) {
            if (s.getsId() == sId){
                tmpS = s;
                break;
            }
        }
        if (tmpS == null){
            System.out.println("Error:没有找到这个学生");
        }else{
            //删除元素
            boolean ret = studentList.remove(tmpS);
            System.out.println(ret ? "删除成功" : "删除失败");
        }
    }

    // 修改
    public void edit(){
        if (noStudent())  return;

        System.out.println("请输入需要修改的学生的学号");
        int sId = sc.nextInt();
        Student tmpS = null;
        for (Student s : studentList) {
            if (s.getsId() == sId){
                tmpS = s;
                break;
            }
        }
        if (tmpS == null){
            System.out.println("Error:没有找到这个学生");
        }else{
            //查询成功后提示重新输入姓名和年龄
            studentList.remove(tmpS);

            System.out.println("请输入该学生的姓名：");
            String newName = sc.next();
            System.out.println("请输入该学生的年龄：");
            int age = sc.nextInt();
            while (age < 0){
                System.out.println("年龄不合理，请重新输入");
                age = sc.nextInt();
            }
            Student newStu = null;

            try {
                newStu = new Student(newName,age,tmpS.getsId());
                boolean ret = studentList.add(newStu);
                System.out.println(ret ? "修改成功" : "修改失败");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("修改失败");
            }
        }
    }

    // 查询
    public void find(){
        if (noStudent())  return;

        System.out.println("请输入需要查询的学生的学号");
        int sId = sc.nextInt();
        Student tmpS = null;
        for (Student s : studentList) {
            if (s.getsId() == sId){
                tmpS = s;
                break;
            }
        }
        if (tmpS == null){
            System.out.println("Error:没有找到这个学生");
        }else{
            //查询成功后打印
            System.out.println("查询成功，结果为：" + tmpS);
        }
    }

    // 遍历所有学生信息
    public void traverse(){
        if (noStudent())  return;

        for (Student s : studentList) {
            System.out.println("学生信息：" + s);
        }
    }

    // 展示功能列表
    public void showMenu(){
        System.out.println("1:增加");
        System.out.println("2:删除");
        System.out.println("3:修改");
        System.out.println("4:遍历所有学生");
        System.out.println("0:退出程序");
        System.out.println("请输入对应数字进入指定功能：");
    }


    // 检查系统中是否有数据
    private boolean noStudent(){
        if (studentList.size() == 0){
            System.out.println("Error:系统中还没有保存过学生信息");
            return true;
        }
        return false;
    }

}
